package com.noreply.springannotation.config;

import com.noreply.springannotation.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * @author noreply
 * @description
 */
// 加上注解才能被IOC容器加载
@Configuration
@ComponentScan(value = "com.noreply.springannotation.bean")
public class UserConfig {
    // 在容器中注册bean，bean的名称是方法名 user ,可以指定bean名称
    @Bean
    public User user() {
        return new User(1L, "noreply");
    }

    // 注册的bean名称是user1
    @Bean
    public User user1() {
        return new User(1L, "noreply1");
    }

    // 注册的bean名称是user2, 不是方法名userUser
    @Bean("user2")
    public User userUser() {
        return new User(1L, "noreply2");
    }

    @Scope("prototype")
    @Bean
    public User prototypeUser() {
        System.out.println("注册user到IOC容器");
        return new User(1L, "name-prototypeUser");
    }

    @Scope("singleton")
    @Bean
    public User singletonUser() {
        System.out.println("注册user到IOC容器");
        return new User(1L, "name-singletonUser");
    }
}
